package com.food.restaurantpro.observers;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.ProgressDialog;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.connection.DataHelper;
import com.food.restaurantpro.connection.DataResult;
import com.food.restaurantpro.managingnetwork.ManagingNetwork;

public class CancelPreOrderObserver implements Observer {

	private ProgressDialog _waitingDialog;
	private CancelPreOrderObserver _me = this;
	private boolean hadRegisterObserver = false;
	private CancelPreOrderObserverInterface mCancelPreOrderObserverInterface;
	
	private Activity mActivity;
	private int mPosition = -1;

	public CancelPreOrderObserver(CancelPreOrderObserverInterface cancelPreOrderObserverInterface) {
		mCancelPreOrderObserverInterface = cancelPreOrderObserverInterface;
	}

	/**
	 * 
	 * @param activity
	 * @param orderID
	 * @param position : vi tri cua pre-order-object tron list pre-order
	 */
	public void registerObserver(Activity activity, String orderID, int position) {
		mActivity = activity;
		mPosition = position;
		if (ManagingNetwork.isNetworkAvailable()) {
			// show progress
			showProcessDialog();
			hadRegisterObserver = true;
			DataHelper.getInstance().addObserver(_me);
			DataHelper.getInstance().cancelPreOrder(orderID);

		} else {
			mCancelPreOrderObserverInterface.cancelPreOrderCallback(-1,
					RestaurantApp.CURRENT_CONTEXT
							.getString(R.string.NETWORK_ERROR));
		}
	}

	/**
	 * must call this method in onDestroy of Activity which called
	 * registerObserver
	 */
	public void unRegisterObserver() {
		if (hadRegisterObserver) {
			hideProcessDialog();
			DataHelper.getInstance().deleteObserver(_me);
			hadRegisterObserver = false;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// close progress
		if (data == null){
			hideProcessDialog();
			return;
		}
			
		DataResult result = (DataResult) data;
		switch (result.getMethodName()) {
		case cancelPreOrder:
			if (result.getErrorCode() != null) {
				// show error mesage
				mCancelPreOrderObserverInterface.cancelPreOrderCallback(-1, result
						.getErrorCode().getErrorMessage());
			} else {
				if (result.getData() != null && "true".equalsIgnoreCase((String)result.getData())) {
					//
					mCancelPreOrderObserverInterface.cancelPreOrderCallback(mPosition, null);
				} else {
					mCancelPreOrderObserverInterface.cancelPreOrderCallback(-1, RestaurantApp.CURRENT_CONTEXT
							.getString(R.string.SERVER_ERROR));
				}
			}
			unRegisterObserver();
			break;
		default:
			break;
		}
		// Chu y: Observer se post notification den tat ca cac lop da register
		// observer. Va cho nao co phuong thuc trung voi phuong thuc no tra ve
		// thi xu ly
		// cho do, nhung cho khac ko xu ly, vi vay PHAI dat code o trong
		// "case method:"
		// khong dat code o ngoai nay
	}

	private void showProcessDialog() {
		if (_waitingDialog == null || !_waitingDialog.isShowing()) {
			_waitingDialog = new ProgressDialog(mActivity);
			_waitingDialog.setMessage(mActivity.getString(R.string.processing));
			_waitingDialog.setCancelable(false);
			_waitingDialog.show();
		}
	}

	//Ham nay phai duoc goi trong onDestroy
	//Vi co truong hop dang request thi destroy activity
	//dialog se ko duoc dong neu ko dat ham nay trong onDestroy
	public void hideProcessDialog() {
		if (_waitingDialog != null && _waitingDialog.isShowing()) {
			_waitingDialog.dismiss();
		}
	}

}
